﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace TestBat.Views.Converters
{
    [ValueConversion(typeof(int), typeof(Color))]
    public class NullPercentage2ColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return Colors.White;
            int p = (int)value;
            byte r = (byte)Math.Min(255, -p * 5.1 + 510);
            byte g = (byte)Math.Min(255, p * 5.1);
            byte b = (p > 50) ? r : g;
            return Color.FromRgb(r, g, b);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.Equals(Colors.White))
                return null;
            Color c = (Color)value;
            if (c.B == c.R)
                return -((int)c.B - 510) / 5.1;
            else
                return (int)c.B / 5.1;
        }
    }
}
